package cn.weiwei.WHadoop.hdfs.filesystem;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

/**
 * @author WangWeiwei
 * @version 1.0
 * @sine 17-2-6
 * 将本地文件复制到HDFS文件系统
 */
public class FileCopyWithProgress {
    public void copyFileToHDFS(String localSrc,String dst) throws Exception{
        InputStream inputStream = new BufferedInputStream(new FileInputStream(localSrc));
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(URI.create(dst),configuration);
        OutputStream outputStream = fileSystem.create(new Path(dst), new Progressable() {
            @Override
            public void progress() {
                System.out.print(".");
            }
        });
        IOUtils.copyBytes(inputStream,outputStream,4096,true);
    }
}
